package com.hbsd.paopaobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbsd.paopaobackend.common.ErrorCode;
import com.hbsd.paopaobackend.exception.BusinessException;
import com.hbsd.paopaobackend.mapper.MessageMapper;
import com.hbsd.paopaobackend.model.domain.Message;
import com.hbsd.paopaobackend.model.domain.User;
import com.hbsd.paopaobackend.model.request.Message.MessageListRequest;
import com.hbsd.paopaobackend.model.vo.Message.MessageListResponse;
import com.hbsd.paopaobackend.service.MessageService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zzh
 * @description 针对表【message】的数据库操作Service实现

 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
        implements MessageService {

    /**
     * 发送消息
     *
     * @param message
     * @param loginUser
     * @return
     */
    @Override
    public Boolean send(Message message, User loginUser) {
        if (message == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "消息不能为空");
        }

        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }
        Long id = loginUser.getId();
        if(id == null || id <= 0) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户不存在");
        }
        message.setFrom_id(id);

        String content = message.getContent();
        if (content == null || content.trim().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "消息内容不能为空");
        }

        boolean save = this.save(message);
        if (!save) {
            throw new BusinessException(ErrorCode.SAVE_ERROR, "消息保存失败");
        }
        return save;
    }


    /**
     * 获取消息列表
     * @param messageListRequest
     * @param loginUser
     * @return
     */
    @Override
    public Page<MessageListResponse> listMessagePage(MessageListRequest messageListRequest, User loginUser) {
        if(messageListRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        }

        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Long fromId = messageListRequest.getFromId();
        Long toId = messageListRequest.getToId();
        System.out.println(" toId = " + toId);
        System.out.println(" fromId = " + fromId);
        long pageNum = messageListRequest.getPageNum();
        long pageSize = messageListRequest.getPageSize();
        if((fromId == null || fromId <= 0) || (toId == null || toId <= 0) || (pageNum <= 0) || (pageSize <= 0)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分页信息转换失败");
        }

        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
//        queryWrapper
//                .eq("from_id", fromId).or().eq("from_id", toId)
//                .and(wrapper -> wrapper.eq("to_id", toId).or().eq("to_id", fromId));
        queryWrapper
                .and(wrapper ->
                        wrapper.eq("from_id", fromId).eq("to_id", toId)
                                .or()
                                .eq("from_id", toId).eq("to_id", fromId)
                );


//        queryWrapper.orderByDesc("createTime");
        Page<Message> page = this.page(new Page<>(pageNum, pageSize), queryWrapper);

        List<MessageListResponse> list = page.getRecords().stream().map(message -> {
            MessageListResponse messageListResponse = new MessageListResponse();
            // 2中方法都可以
//            BeanUtil.copyProperties(message, messageListResponse);
            try {
                BeanUtils.copyProperties(messageListResponse, message);
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "消息转换失败");
            }
            return messageListResponse;
        }).collect(Collectors.toList());

        Page<MessageListResponse> messageListPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        messageListPage.setRecords(list);

        return messageListPage;

    }
}




